package com.uenpay.im.ui.old.activity.company;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.qiyunxin.android.http.utils.Utils;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.callback.IOnBottomDialogListener;
import com.uenpay.im.weight.DialogUtility;

/**
 * @author SL
 * @created 2016-2-22 14:40:01
 * @copyRight http://www.qiyunxin.com
 */
public class CreateDepartmentsActivity extends Activity {

	private CompanyHandle mCompanyHandle = new CompanyHandle();
	private EditText department_name_et;
	private TextView superior_department_tv;
	// private int entid, pid;
	private View loading;
	private Button delete_department_btn;
	private boolean is_manager = false;
	private DepartmentEntity departmentEntity = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		IMApplication.getInstance().addActivity(this);
		setContentView(R.layout.activity_create_department_layout);
		departmentEntity = (DepartmentEntity) getIntent().getSerializableExtra(
				"departmentEntity");

		// entid = getIntent().getIntExtra("entid", 0);
		// pid = getIntent().getIntExtra("pid", 0);
		// String superior_department = getIntent().getStringExtra(
		// "superior_department");
		is_manager = getIntent().getBooleanExtra("is_manager", false);
		// String departname = getIntent().getStringExtra("departname");
		initView();
		initListener();
		Utils.showSoftKeyboard(department_name_et);
		if (departmentEntity != null
				&& !TextUtils.isEmpty(departmentEntity.pdepartname))
			superior_department_tv.setText(departmentEntity.pdepartname);
		if (!TextUtils.isEmpty(departmentEntity.departname)) {
			department_name_et.setText(departmentEntity.departname);
		}
	}

	private void initView() {
		((TextView) findViewById(R.id.title_textview))
				.setText(R.string.add_department);

		superior_department_tv = (TextView) findViewById(R.id.superior_department_tv);
		department_name_et = (EditText) findViewById(R.id.department_name_et);
		loading = findViewById(R.id.loading);
		((TextView) findViewById(R.id.title_right_tv)).setText(R.string.save);
		findViewById(R.id.title_right_layout).setVisibility(View.VISIBLE);
		delete_department_btn = (Button) findViewById(R.id.delete_department_btn);
		if (is_manager) {
			((TextView) findViewById(R.id.title_textview))
					.setText(R.string.edit_department);
			delete_department_btn.setVisibility(View.VISIBLE);
		}
	}

	private void initListener() {
		findViewById(R.id.back_layout).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						Utils.hideSoftKeyboard(department_name_et);
						finish();
					}
				});
		findViewById(R.id.title_right_layout).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						String departmentName = department_name_et.getText()
								.toString();
						if (!TextUtils.isEmpty(departmentName)
								&& departmentEntity.entid != 0) {
							Utils.hideSoftKeyboard(department_name_et);
							if (!is_manager) {
								createDepartment(departmentName);
							} else {
								updateDepartment(departmentName);
							}
						}
					}
				});
		delete_department_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (departmentEntity != null && departmentEntity.departid != 0) {
					deleteDepartmentDialog();
				}
			}
		});
	}

	private void deleteDepartmentDialog() {
		Utils.hideSoftKeyboard(department_name_et);
		DialogUtility.showDialog(CreateDepartmentsActivity.this,
				R.string.delete_department_dialog, R.string.sure,
				R.string.cancel, true, new IOnBottomDialogListener() {

					@Override
					public void onClicked() {
						deleteDepartment();
					}
				});
	}

	private void deleteDepartment() {
		loading.setVisibility(View.VISIBLE);
		mCompanyHandle.deleteDepartment(departmentEntity.entid,
				departmentEntity.departid, new CompanyHandle.ICompanyResultListener() {

					@Override
					public void onResult(int status, String msg) {

						loading.setVisibility(View.GONE);
						if (status == HttpInvokeResultCode.RESULT_OK) {
							setResult(2);
							finish();
						} else if (TextUtils.isEmpty(msg)) {
							IMApplication.showToast(msg);
						}

					}
				});
	}

	private void createDepartment(String departmentName) {
		loading.setVisibility(View.VISIBLE);
		mCompanyHandle.createDepartment(departmentEntity.entid,
				departmentEntity.pid, departmentName,
				new CompanyHandle.ICompanyResultListener() {

					@Override
					public void onResult(int status, String msg) {
						loading.setVisibility(View.GONE);
						if (status == HttpInvokeResultCode.RESULT_OK) {
							setResult(RESULT_OK);
							finish();
						} else if (TextUtils.isEmpty(msg)) {
							IMApplication.showToast(msg);
						}
					}
				});
	}

	private void updateDepartment(String departname) {
		loading.setVisibility(View.VISIBLE);
		mCompanyHandle.updateDepartment(departmentEntity.departid,
				departmentEntity.entid, departmentEntity.pid, departname,
				new CompanyHandle.ICompanyResultListener() {

					@Override
					public void onResult(int status, String msg) {
						loading.setVisibility(View.GONE);
						if (status == HttpInvokeResultCode.RESULT_OK) {
							setResult(RESULT_OK);
							finish();
						} else if (TextUtils.isEmpty(msg)) {
							IMApplication.showToast(msg);
						}
					}
				});
	}

	@Override
	public void onBackPressed() {
		Utils.hideSoftKeyboard(department_name_et);
		finish();
		super.onBackPressed();
	}

	@Override
	protected void onDestroy() {
		IMApplication.getInstance().removeActivity(this);
		super.onDestroy();
	}
}
